package com.soft.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.soft.entity.Live;
import com.soft.entity.Worker;
import com.soft.mapper.LiveMapper;
import com.soft.mapper.WorkerMapper;

@Service
public class WorkerService {
	@Autowired
	private WorkerMapper workerMapper;
	@Autowired
    private LiveMapper liveMapper;
	private Object liveAddress;
	public List<Worker> queryAll(){
		List<Worker> workerList = workerMapper.selectList(null);
		return workerList;
	}
	
	public List<Worker> queryBySex(String workerSex){
		QueryWrapper<Worker> qw= new QueryWrapper<>();
		qw.eq("worker_sex", workerSex);
		List<Worker> workerList = workerMapper.selectList(qw);
		return workerList;
	}
	
	public int insertOne(String workerName,String workerSex,String workerPhone){
		Worker worker = new Worker();
		worker.setWorkerName(workerName);
		worker.setWorkerSex(workerSex);
		worker.setWorkerPhone(workerPhone);
		int num = workerMapper.insert(worker);
		return num;
	}
	
	public int deleteById(long workerId){
		Worker worker = new Worker();
		worker.setWorkerId(workerId);
		int num = workerMapper.deleteById(worker);
		return num;
	}
	
	public int update(Long workId,String workerName,String workerSex,String workerPhone){
		Worker worker = new Worker();
		worker.setWorkerId(workId);
		worker.setWorkerName(workerName);
		worker.setWorkerSex(workerSex);
		worker.setWorkerPhone(workerPhone);
		int num = workerMapper.updateById(worker);
		return num;
	}
	
	public List<Worker> queryByWorkerLiveAddress(String liveAddress) {
	    QueryWrapper<Live> liveWrapper = new QueryWrapper<>();
	    liveWrapper.eq("live_address", liveAddress);
		Live live = liveMapper.selectOne(liveWrapper);
	    QueryWrapper<Worker> workerWrapper = new QueryWrapper<>();
	    workerWrapper.eq("worker_liveid", live.getLiveId());
	    return workerMapper.selectList(workerWrapper);
	}
	
	public int insertWorker(String workerName,String workerSex,String workerPhone,String liveAddress) {
		QueryWrapper<Live> liveWrapper = new QueryWrapper<>();
		liveWrapper.eq("live_address",liveAddress);
		Live live = liveMapper.selectOne(liveWrapper);
		Worker worker = new Worker();
		worker.setWorkerName(workerName);
		worker.setWorkerSex(workerSex);
		worker.setWorkerPhone(workerPhone);
		worker.setWorkerLiveid(live.getLiveId());
		int num = workerMapper.insert(worker);
		return num;
	}
	
	public int updateWorker(String workerName,String workerSex,String workerPhone,String liveAddress) {
		QueryWrapper<Live> liveWrapper = new QueryWrapper<>();
		liveWrapper.eq("live_address",liveAddress);
		Live live = liveMapper.selectOne(liveWrapper);
		UpdateWrapper<Worker> workerWrapper = new UpdateWrapper<>();
		workerWrapper.eq("worker_name", workerName);
		workerWrapper.eq("worker_liveid", live.getLiveId());
		Worker worker = new Worker();
		worker.setWorkerSex(workerSex);
		worker.setWorkerPhone(workerPhone);
		int num = workerMapper.update(worker,workerWrapper);
		return num;
	}
}
